package test.t1;

import com.maxmind.db.Reader;
import com.maxmind.db.Reader.FileMode;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Map;

public class MmdbReader {
    public Reader reader;
    public void init(String dbPath) throws IOException {
        var database = new File(dbPath);
        if (!database.exists()) {
            throw new IllegalArgumentException("MMDB 数据库文件不存在: $dbPath");
        }
        reader = new Reader(database, FileMode.MEMORY);
    }

    public Map<String, Object> lookup(String ip) throws IOException {
        var ipAddress = InetAddress.getByName(ip);
        return reader.get(ipAddress, Map.class);
    }


}
